require 'fileutils'
require 'pyro/helpers'
require 'pyro/rest'
require 'sinatra/base'

# Loads environment config from .env
Dotenv.load

# Namespaces Pyro::App.
module Pyro
  # Public: The main Pyro application.
  #
  # Example
  #
  #   Pyro::App.run!
  class App < Sinatra::Application
    # Hot reload in the development environment.
    require 'sinatra/reloader' unless production?

    # Basic settings.
    set :root, '.'
    set :public_folder, './public'
    set :port, 7976
    set :sessions,
         httponly: true,
         secure: production?,
         expire_after: 31_557_600, # 1 year
         secret: ENV['SESSION_SECRET']

    # Load the Pyro helpers.
    helpers Pyro::Helpers

    # Gzip responses at runtime.
    use Rack::Deflater

    # Load the Pyro RESTful JSON API routes.
    use REST::API

    # Read /public/index.html on GET / requests.
    #
    # * Re-render before each request when running in
    #   any environment except production.
    get '/?' do
      render() unless settings.environment == :production
      File.read "#{settings.public_folder}/index.html"
    end
  end
end